// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 
package commande;

import com.lloseng.ocsf.client.AbstractClient;
import com.lloseng.ocsf.common.ChatIF;
import java.io.IOException;

/**
 * This class overrides some of the methods defined in the abstract superclass
 * in order to give more functionality to the client.
 *
 * @author Dr Timothy C. Lethbridge
 * @author Dr Robert Lagani&egrave;
 * @author Fran&ccedil;ois B&eacute;langer
 * @version July 2000
 */
public class ChatClient extends AbstractClient {
    //Instance variables **********************************************

    /**
     * The interface type variable. It allows the implementation of the display
     * method in the client.
     */
    ChatIF clientUI;
   
    /**
     * Cette variable permet de parcourir notre chaine de reconaissance de
     * commande
     *
     */
    private IAnalysateurTexte analysateurTexte;

    //Constructors ****************************************************
    /**
     * Constructs an instance of the chat client.
     *
     * @param host The server to connect to.
     * @param port The port number to connect on.
     * @param clientUI The interface type variable.
     */
    public ChatClient(String host, int port, ChatIF clientUI) throws IOException {
        super(host, port); //Call the superclass constructor
        this.clientUI = clientUI;

        this.analysateurTexte = new Reconnait_SetHost(new Reconnait_Login(
                new Reconnait_SetPort(
                new Reconnait_GetPort(
                new Reconnait_GetHost(
                new Reconnait_Logoff(
                new Reconnait_Quit(null, this), this), this), this), this), this),this);

    }

    //Instance methods ************************************************
    /**
     * This method handles all data that comes in from the server.
     *
     * @param msg The message from the server.
     */
    public void handleMessageFromServer(Object msg) {
        clientUI.display(msg.toString());
    }

    /*
     * Cette m�thode d�termine si le message du client est une commande ou un
     * simple texte, et charge des m�thodes pour effectuer le traitement ad�quat
     */
    public void handleMessageFromClientUI(String message) throws IOException {
        if (message.trim().startsWith("#")) {//NONONO
            try {
                this.analysateurTexte.ChaineAnalyseTexte(message);
            } catch (SaisieErroneeException e) {
                this.clientUI.display("Commande invalide :" + e.getMessage());
            }
        } else {
            this.sendToServer(message);
        }
    }

    /**
     * This method terminates the client.
     */
    public void quit() {
        try {
            closeConnection();
        } catch (IOException e) {
        }
        System.exit(0);
    }

    @Override
    protected void connectionClosed() {
        this.clientUI.display("La connection a ete ferme");
    }

    @Override
    protected void connectionException(Exception exception) {
        this.clientUI.display(exception.getMessage());
    }
    
    
}
//End of ChatClient class
